#include <glib.h>

typedef struct my_data_tag {
    gint id;
    gchar *name;
}my_data_t;

static void _dataset_foreach_func(GQuark key_id, gpointer data, gpointer user_data)
{
    my_data_t *p = (my_data_t *)data;

    g_print("[foreach] key:%s, id: %d, name: %s, user_data: %s \n",  \
        g_quark_to_string(key_id), p->id, p->name, (gchar *)user_data);
}

static void _dataset_id_destroy_func(gpointer data)
{
    my_data_t *p = (my_data_t *)data;
    if(NULL == p) {
        return;
    }

    g_print("destroy %s! \n", p->name);

    g_free(p->name);
    g_free(p);
}

static void glib_dataset_set_get_test (void)
{
    gpointer location = (gpointer)glib_dataset_set_get_test;

    my_data_t *data1 = NULL;
    my_data_t *data2 = NULL;
    my_data_t *data3 = NULL;
    my_data_t *data4 = NULL;
    my_data_t *ret = NULL;

    data1 = g_new0(my_data_t, 1);
    data1->id = 101;
    data1->name = g_strdup_printf("name:%d", data1->id);
    data2 = g_new0(my_data_t, 1);
    data2->id = 102;
    data2->name = g_strdup_printf("name:%d", data2->id);
    data3 = g_new0(my_data_t, 1);
    data3->id = 103;
    data3->name = g_strdup_printf("name:%d", data3->id);
    data4 = g_new0(my_data_t, 1);
    data4->id = 104;
    data4->name = g_strdup_printf("name:%d", data4->id);

    g_print("data2->id(%p): %d \n", &data2->id, data2->id);
    g_print("data2->name(%p): %s \n", data2->name, data2->name);

    g_dataset_id_set_data_full(location, g_quark_from_string ("one"), data1, _dataset_id_destroy_func);
    g_dataset_id_set_data_full(location, g_quark_from_string ("two"), data2, _dataset_id_destroy_func);
    g_dataset_id_set_data_full(location, g_quark_from_string ("three"), data3, _dataset_id_destroy_func);

    g_dataset_foreach(location, _dataset_foreach_func, (gpointer)"foreach");
    
    g_print("before set_data_full: two \n");
    g_dataset_id_set_data_full(location, g_quark_from_string ("two"), data4, _dataset_id_destroy_func);
    g_print("after set_data_full: two \n");

    ret = g_dataset_id_get_data (location, g_quark_from_string ("two"));

    g_print("ret->id(%p): %d \n", &ret->id, ret->id);
    g_print("ret->name(%p): %s \n", ret->name, ret->name);

    g_dataset_destroy (location);
}

gint main (gint argc, gchar** argv)
{
    glib_dataset_set_get_test();

    return 0;
}
